<template>
  <div class="user-info">
    <el-tooltip effect="dark" content="打印个人基本信息" placement="bottom">
      <i class="el-icon-printer" @click="$router.push({ name:'print', query: {type: 'personal', id: $route.query.id} })" />
    </el-tooltip>
    <!-- 个人信息 -->
    <el-form label-width="120px">
      <!-- 工号 入职时间 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input v-model="userInfo.username" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工号">
            <el-input v-model="userInfo.workNumber" clearable />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker v-model="userInfo.timeOfEntry" type="date" value-format="YYYY-MM-DD" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门">
            <el-input v-model="userInfo.departmentName" autocomplete="off" clearable @focus="deptFocus" @blur="deptBlur" />
            <el-tree v-show="treeShow" v-loading="treeLoading" :default-expand-all="true" :data="treeData" :props="defaultProps" @node-click="handleNodeClick" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 姓名 部门 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="聘用形式">
            <el-select v-model="userInfo.formOfEmployment">
              <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <ImageUpload :img-url.sync="userInfo.staffPhoto" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row type="flex" justify="center">
        <el-button type="primary" @click="saveUser">保存更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-row>
    </el-form>
    <el-form label-width="124px">
      <!-- 基础信息 -->
      <el-divider>基本信息</el-divider>
      <!-- 员工照片 -->
      <el-row>
        <el-col :span="24">
          <el-form-item label="员工照片">
            <ImageUpload :img-url.sync="formData.staffPhoto" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 最高学历 国家/地区 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="最高学历">
            <el-select v-model="formData.theHighestDegreeOfEducation">
              <el-option v-for="item in EmployeeEnum.highestDegree" :key="item.id" :label="item.value" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="国家/地区">
            <el-select v-model="formData.nationalArea">
              <el-option v-for="item in EmployeeEnum.isOverseas" :key="item.id" :label="item.value" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 护照号 身份证号 -->
      <el-row>
        <el-col :span="formData.passportNo ? 24 : 12">
          <el-form-item v-show="!formData.idNumber" label="护照号">
            <el-input v-model="formData.passportNo" placeholder="正规护照格式" />
          </el-form-item>
        </el-col>
        <el-col :span="formData.idNumber ? 24 : 12">
          <el-form-item v-show="!formData.passportNo" label="身份证号">
            <el-input v-model="formData.idNumber" placeholder="正规身份证格式" @input="handleChange" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 籍贯 户籍所在地 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="籍贯">
            <el-cascader v-model="temp1" :props="props" separator=" " placeholder="请选择" clearable @visible-change="visibleChange" />
            <!-- <el-input v-model="formData.nativePlace" placeholder="籍贯地址" /> -->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="户籍所在地">
            <el-cascader v-model="temp2" :props="props" separator=" " placeholder="请选择" clearable @visible-change="visibleChange" />
            <!-- <el-input v-model="formData.domicile" placeholder="请输入" /> -->
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 民族 星座 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="民族">
            <el-input v-model="formData.nation" placeholder="请输入民族" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别">
            <el-select v-model="formData.sex">
              <el-option v-for="item in EmployeeEnum.gender" :key="item.id" :label="item.value" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 年龄 生日 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="年龄">
            <el-input v-model="formData.age" type="number" placeholder="请输入员工年龄" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生日">
            <el-input v-model="formData.birthday" placeholder="示例 0323" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 血型 婚姻状况 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="血型">
            <el-select v-model="formData.bloodType">
              <el-option v-for="item in EmployeeEnum.bloodType" :key="item.id" :label="item.value" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="星座">
            <el-select v-model="formData.constellation">
              <el-option v-for="item in EmployeeEnum.constellation" :key="item.code" :label="item.value" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 政治面貌 入党时间 存档机构 -->
      <el-row>
        <el-col :span="formData.politicalOutlook == '13' ? 24 : 12">
          <el-form-item label="政治面貌">
            <el-select v-model="formData.politicalOutlook" placeholder="请选择">
              <el-option v-for="item in EmployeeEnum.politicalOutlook" :key="item.id" :label="item.value" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-show="formData.politicalOutlook !== '13'" :span="12">
          <el-form-item label="入党时间">
            <el-date-picker v-model="formData.timeToJoinTheParty" type="date" placeholder="选择入党日期" value-format="yyyy-MM-dd" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="存档机构">
            <el-input v-model="formData.archivingOrganization" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="婚姻状况">
            <el-select v-model="formData.maritalStatus">
              <el-option v-for="item in EmployeeEnum.maritaStatus" :key="item.id" :label="item.value" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 子女状态 子女有无商业险 -->
      <el-row v-show="formData.maritalStatus !== '1'">
        <el-col :span="12">
          <el-form-item label="子女状态">
            <el-input v-model="formData.stateOfChildren" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="子女有无商业险">
            <el-radio-group v-model="formData.doChildrenHaveCommercialInsurance">
              <el-radio label="1">有</el-radio>
              <el-radio label="2">无</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 有无违法违纪状态 有无重大病史 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="有无违法违纪状态">
            <el-input v-model="formData.isThereAnyViolationOfLawOrDiscipline" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="有无重大病史">
            <el-input v-model="formData.areThereAnyMajorMedicalHistories" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 通讯信息 -->
      <el-divider>通讯信息</el-divider>
      <!-- QQ 微信 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="QQ">
            <el-input v-model="formData.qq" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="微信">
            <el-input v-model="formData.wechat" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 现居住地 通讯地址 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="现居住地">
            <el-input v-model="formData.placeOfResidence" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="通讯地址">
            <el-input v-model="formData.postalAddress" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 联系手机 个人邮箱 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="联系手机">
            <el-input v-model="formData.contactTheMobilePhone" placeholder="11位字符" maxlength="11" @change.native="handlePhone(2)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="个人邮箱">
            <el-input v-model="formData.personalMailbox" placeholder="请输入" type="mail" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 紧急联系人 紧急联系电话 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="紧急联系人">
            <el-input v-model="formData.emergencyContact" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="紧急联系电话">
            <el-input v-model="formData.emergencyContactNumber" placeholder="11位字符" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 账号信息 -->
      <el-divider>账号信息</el-divider>
      <el-row>
        <el-col :span="12">
          <el-form-item label="社保电脑号">
            <el-input v-model="formData.socialSecurityComputerNumber" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公积金账号">
            <el-input v-model="formData.providentFundAccount" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 银行卡号 开户行 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="银行卡号">
            <el-input v-model="formData.bankCardNumber" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开户行">
            <el-input v-model="formData.openingBank" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 教育信息 -->
      <el-divider>教育信息</el-divider>
      <el-row>
        <el-col :span="12">
          <el-form-item label="学历类型">
            <el-select v-model="formData.educationalType" placeholder="请选择">
              <el-option v-for="item in EmployeeEnum.educationType" :key="item.id" :label="item.value" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="专业">
            <el-input v-model="formData.major" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="入学时间">
            <el-date-picker v-model="formData.enrolmentTime" type="data" placeholder="请输入时间" value-format="yyyy-MM-dd" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="毕业时间">
            <el-date-picker v-model="formData.graduationTime" type="data" placeholder="请输入时间" value-format="yyyy-MM-dd" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="毕业学校">
            <el-input v-model="formData.graduateSchool" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="毕业证书号">
            <el-input v-model="formData.graduationCertificate" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 从业信息 -->
      <el-divider>从业信息</el-divider>
      <!-- 上家公司 职称 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="上家公司">
            <el-input v-model="formData.homeCompany" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="职称">
            <el-input v-model="formData.title" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 有无竞业限制 备注 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="有无竞业限制">
            <el-input v-model="formData.isThereAnyCompetitionRestriction" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="简历">
            <el-input v-model="formData.resume" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 备注 -->
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input v-model="formData.remarks" :rows="6" resize="none" type="textarea" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存员工信息 -->
      <el-row type="flex" justify="center">
        <el-button type="primary" @click="savePersonal">保存更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import EmployeeEnum from '@/utils/constant/employees'
import Axios from 'axios'
import { arrayToTree } from '@/utils'
import { getDepts } from '@/api/departments'
import { userProfile } from '@/api/user'
import { getEmpPersonInfo, saveUserInfo, saveEmpPersonalInfo } from '@/api/employees'
export default {
  name: 'EmployessPersonInfo',
  data() {
    return {
      userId: this.$route.query.id,
      EmployeeEnum, // 员工枚举数据
      userInfo: {
        workNumber: '',
        timeOfEntry: '',
        username: '',
        departmentName: '',
        mobile: '',
        formOfEmployment: '',
        staffPhoto: ''
      },
      formData: {
        userId: '',
        username: '', // 用户名
        sex: '', // 性别
        mobile: '', // 手机
        companyId: '', // 公司id
        departmentName: '', // 部门名称
        //  onTheJobStatus: '', // 在职状态 no
        dateOfBirth: '', // 出生日期
        timeOfEntry: '', // 入职时间
        theHighestDegreeOfEducation: '', // 最高学历
        nationalArea: '', // 国家
        passportNo: '', // 护照号
        idNumber: '', // 身份证号
        idCardPhotoPositive: '', // 身份证照正
        idCardPhotoBack: '', // 身份证照正
        nativePlace: '', // 籍贯
        nation: '', // 民族
        englishName: '', // 英文名字
        maritalStatus: '', // 婚姻状况
        staffPhoto: '', // 员工照片
        birthday: '', // 生日
        zodiac: '', // 属相
        age: '', // 年龄
        constellation: '', // 星座
        bloodType: '', // 血型
        domicile: '', // 户籍所在地
        politicalOutlook: '', // 政治面貌
        timeToJoinTheParty: '', // 入党时间
        archivingOrganization: '', // 存档机构
        stateOfChildren: '', // 子女状态
        doChildrenHaveCommercialInsurance: '1', // 保险状态
        isThereAnyViolationOfLawOrDiscipline: '', // 违法违纪状态
        areThereAnyMajorMedicalHistories: '', // 重大病史
        qq: '', // QQ
        wechat: '', // 微信
        residenceCardCity: '', // 居住证城市
        dateOfResidencePermit: '', // 居住证办理日期
        residencePermitDeadline: '', // 居住证截止日期
        placeOfResidence: '', // 现居住地
        postalAddress: '', // 通讯地址
        contactTheMobilePhone: '', // 联系手机
        personalMailbox: '', // 个人邮箱
        emergencyContact: '', // 紧急联系人
        emergencyContactNumber: '', // 紧急联系电话
        socialSecurityComputerNumber: '', // 社保电脑号
        providentFundAccount: '', // 公积金账号
        bankCardNumber: '', // 银行卡号
        openingBank: '', // 开户行
        educationalType: '', // 学历类型
        graduateSchool: '', // 毕业学校
        enrolmentTime: '', // 入学时间
        graduationTime: '', // 毕业时间
        major: '', // 专业
        graduationCertificate: '', // 毕业证书
        certificateOfAcademicDegree: '', // 学位证书
        homeCompany: '', // 上家公司
        title: '', // 职称
        resume: '', // 简历
        isThereAnyCompetitionRestriction: '', // 有无竞业限制
        proofOfDepartureOfFormerCompany: '', // 前公司离职证明
        remarks: '' // 备注
      },
      props: {
        lazy: true,
        expandTrigger: 'hover',
        lazyLoad(node, resolve) {
          Axios.get('/gaodeMapAPI/v3/config/district', {
            params: {
              keywords: node.value,
              subdistrict: '1',
              key: 'f7c1ee410753e832aad78bc5cf135351'
            }
          }).then(res => {
            const nodes = res.data.districts[0].districts.map(item => {
              return {
                value: item.name,
                label: item.name,
                leaf: item.level === 'district'
              }
            })
            resolve(nodes)
          })
        }
      },
      defaultProps: {
        label: 'name'
      },
      treeData: [],
      treeLoading: false,
      treeShow: false,
      temp1: '',
      temp2: ''
    }
  },
  async created() {
    const uesrResulr = await userProfile(this.$route.query.id)
    this.userInfo = uesrResulr.data
    this.userInfo.formOfEmployment == '1' ? this.userInfo.formOfEmployment = '正式' : this.userInfo.formOfEmployment = '非正式'
    const empResult = await getEmpPersonInfo(this.$route.query.id)
    // this.temp1 = empResult.data.nativePlace.split(' ')
    this.temp1 = empResult.data.nativePlace ? empResult.data.nativePlace.split(' ') : ''
    this.temp2 = empResult.data.nativePlace ? empResult.data.nativePlace.split(' ') : ''
    // this.temp2 = empResult.data.domicile.split(' ')
    this.formData = empResult.data
  },
  methods: {
    async saveUser() {
      this.userInfo.formOfEmployment == '正式' ? this.userInfo.formOfEmployment = 1 : this.userInfo.formOfEmployment = 2
      await saveUserInfo(this.userInfo)
    },
    async savePersonal() {
      this.formData.nativePlace = this.temp1.join(' ')
      this.formData.domicile = this.temp2.join(' ')
      await saveEmpPersonalInfo(this.formData)
    },
    async deptFocus() {
      this.treeShow = true
      this.treeLoading = true
      const result = await getDepts()
      this.treeData = arrayToTree(result.data.depts, '')
      this.treeLoading = false
    },
    deptBlur(event) {
      if (event.relatedTarget == null || event.relatedTarget.className.includes(!'el-tree-node')) {
        this.treeShow = false
      }
    },
    handleNodeClick(data) {
      this.userInfo.departmentName = data.name
      this.treeShow = false
    },
    visibleChange() {
      this.$nextTick(() => {
        const $el = document.querySelectorAll('.el-cascader-panel .el-cascader-node[aria-owns]')
        Array.from($el).map((item) => item.removeAttribute('aria-owns'))
      })
    },
    handleChange(e) {
      const birth = e.substring(10, 14)
      const now = new Date()
      const year = now.getFullYear()
      this.formData.birthday = birth
      this.formData.age = year - e.substring(6, 10)
    }
  }
}
</script>

<style lang="scss" scoped>
.user-info {
  .el-select,
  .el-input,
  .el-cascader {
    width: 100%;
  }
  .el-tree {
    padding: 6px 0;
    position: absolute;
    top: 44px;
    z-index: 1;
    width: 100%;
    height: 176px;
    border: 1px solid #d7dae2;
    border-radius: 4px;
    box-shadow: 0 -3em 3em rgba(116, 113, 113, 0.1), 0.3em 0.3em 1em rgba(0,0,0,0.3);
    overflow-y: scroll;
  }
}
</style>
